home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16558 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  75 lines

  1. Path: ix.netcom.com!news
  2. From: giuliano@ix.netcom.com(Giuliano Carlini)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Mutually referring header files
  5. Date: 11 Apr 1996 06:36:19 GMT
  6. Organization: Netcom
  7. Message-ID: <4ki993$1ov@dfw-ixnews2.ix.netcom.com>
  8. References: <4ked7q$8g3@dub-news-svc-1.compuserve.com>
  9. NNTP-Posting-Host: lbx-ca7-14.ix.netcom.com
  10. X-NETCOM-Date: Thu Apr 11  1:36:19 AM CDT 1996
  11.  
  12. In <4ked7q$8g3@dub-news-svc-1.compuserve.com> RossBoylan@aol.com (Ross
  13. Boylan) writes: 
  14. >
  15. >I have 2 classes A + B, defined in separate files.  Each refers to and
  16. >messages the other.  What is the best way to handle this?
  17. > ...
  18. >3) It may or may not matter that I'm actually using smart pointers, so
  19. >the pointer declarations are actually
  20. >CountedObjPtr<A> pA;
  21. >
  22. >4) 
  23. >Also, I put no code definition in the header file (i.e., b.h contains
  24. >no lines of the form
  25. >class B {
  26. >    void doit() {pA->hello();};
  27. >};
  28. >Such lines require more than class A;--they must actually know the
  29. >protocols A responds to.
  30. >
  31. >This bit of code discipline is possible for regular classes, but what
  32. >do I do for templates.  With my compiler (MSVC++ 4.0) the header file
  33. >must include all the code definition.
  34.  
  35. I'm not sure about the VC++ part of your question, but I'd do it
  36. something like this. I'm always getting my template syntax wrong,
  37. so beware of my stupid errors.
  38.  
  39. // a.h
  40. #ifndef derived_h
  41. #define derived_h
  42.  
  43. #include "base.h"       // must include your base classes header.
  44. template<...> class X;  // Try to avoid including any others.
  45.  
  46. template<...> class Derived : Base<...> {
  47.     void foo( const X& ); // Try to use only Ref's and pointers to
  48.     void bar( const X* ); // other classes. Then you don't need to
  49.                           // include their headers
  50. }
  51.  
  52. #endif // derived_h
  53.  
  54. // derived.t
  55. #ifndef derived_t
  56. #define derived_t
  57.  
  58. #include "x.h"
  59. #include "x.t"
  60.  
  61. template< ... > Derived::foo( const X& )
  62. {
  63.     ...
  64. }
  65.  
  66. #endif // derived_t
  67.  
  68. // client.cpp
  69. #include "derived.h"
  70. #include "derived.t"
  71. ..
  72.  
  73.  
  74. g
  75.